<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="domain.entity.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<script src="<%=basePath%>/Plugins/My97DatePicker/WdatePicker.js"></script>
<script src="<%=basePath%>/Scripts/jquery.blockUI.js"></script>

   <!--  @*特殊休息日*@ -->
    <input type="hidden" name="hdnWwdata1" id="hdnWwdata1" value="" />
    <input type="hidden" name="hdnWwdata2" id="hdnWwdata2" value="" />
    <input type="hidden" name="hdnWwdata3" id="hdnWwdata3" value="" />
    <input type="hidden" name="hdnWwdata4" id="hdnWwdata4" value="" />
    <input type="hidden" name="hdnWwdata5" id="hdnWwdata5" value="" />
    <input type="hidden" name="hdnWwdata6" id="hdnWwdata6" value="" />
    <input type="hidden" name="hdnWwdata7" id="hdnWwdata7" value="" />
    <input type="hidden" name="hdnWwdata8" id="hdnWwdata8" value="" />
    <input type="hidden" name="hdnWwdata9" id="hdnWwdata9" value="" />
    <input type="hidden" name="hdnWwdata10" id="hdnWwdata10" value="" />
    <input type="hidden" name="hdnWwdata11" id="hdnWwdata11" value="" />
    <input type="hidden" name="hdnWwdata12" id="hdnWwdata12" value="" />
  <!--   @*特殊工作日*@ -->
    <input type="hidden" name="hdnWdata1" id="hdnWdata1" value="" />
    <input type="hidden" name="hdnWdata2" id="hdnWdata2" value="" />
    <input type="hidden" name="hdnWdata3" id="hdnWdata3" value="" />
    <input type="hidden" name="hdnWdata4" id="hdnWdata4" value="" />
    <input type="hidden" name="hdnWdata5" id="hdnWdata5" value="" />
    <input type="hidden" name="hdnWdata6" id="hdnWdata6" value="" />
    <input type="hidden" name="hdnWdata7" id="hdnWdata7" value="" />
    <input type="hidden" name="hdnWdata8" id="hdnWdata8" value="" />
    <input type="hidden" name="hdnWdata9" id="hdnWdata9" value="" />
    <input type="hidden" name="hdnWdata10" id="hdnWdata10" value="" />
    <input type="hidden" name="hdnWdata11" id="hdnWdata11" value="" />
    <input type="hidden" name="hdnWdata12" id="hdnWdata12" value="" />

<div id="panl_selectyear">
    请选择要设置的年份：
    <input type="radio" name="selectyears" value="2016" />2016 <input type="radio" name="selectyears" value="2017" />2017 <input type="radio" name="selectyears" value="2018" />2018 &nbsp;&nbsp;&nbsp;&nbsp;
  
</div>
<a href="javascript:saveworkdate()" class="easyui-linkbutton">完成设置工作(已经设置完了)</a>&nbsp;&nbsp;&nbsp;&nbsp;
<br />
当前已经选中日期：<input name="txtSelectDate" type="text" id="txtSelectDate" />
<a href="javascript:AddWwDate_Click()" class="easyui-linkbutton" >设置为节假日</a>

<a href="javascript:AddWDate_Click()" class="easyui-linkbutton">设置为工作日</a>

<div id="autodiv_input">
    <table>
        <tr>
            <td>
                <div id="month1">
                </div>
            </td>
            <td>
                <div id="month2">
                </div>
            </td>
            <td>
                <div id="month3">
                </div>
            </td>
            <td>
                <div id="month4">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="month5">
                </div>
            </td>
            <td>
                <div id="month6">
                </div>
            </td>
            <td>
                <div id="month7">
                </div>
            </td>
            <td>
                <div id="month8">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="month9">
                </div>
            </td>
            <td>
                <div id="month10">
                </div>
            </td>
            <td>
                <div id="month11">
                </div>
            </td>
            <td>
                <div id="month12">
                </div>
            </td>
        </tr>
    </table>

</div>
<script type="text/javascript">
    //     $(document).ready(function() { initCustomEdit();});
    var currentIframeDocument = null;
    var currentDataY = "2016";//当前设置年份
    var currentDataM = null;
    var currentDataD = null;

    $(document).ready(function () {
        //重新打开页面
        $("[name='selectyears']").click(function () {
            currentDataY = $('input:radio[name="selectyears"]:checked').val();
            init(currentDataY);
            $('#panl_selectyear').hide();
        });
        //init(currentDataY);
    });

    function init(year) {
        initedata(year);
        //$.blockUI();
        InitDate();
        setTimeout("InitSelect()", 2000);
    }
    //初始化12个日历
    function InitDate() {
        for (var i = 1; i <= 12; i++) {
            WdatePicker({
                eCont: 'month' + i,
                startDate: currentDataY + '-' + i + '-1',
                minDate: currentDataY + '-' + i + '-1',
                maxDate: currentDataY + '-' + i + '-%ld',
                onpicked: function (dp) {
                    $("#txtSelectDate").val(dp.cal.getDateStr('yyyy-MM-dd'));
                    currentDataM = dp.cal.getDateStr('M');
                    currentDataD = dp.cal.getDateStr('d');
                    currentIframeDocument = $($("iframe")[parseInt(currentDataM) - 1].contentDocument)
                    UpdatePickerWwday(currentDataD);
                }
            });
            WdatePicker();
        }
    }
    //更新选中项
    function UpdatePickerWwday(obj) {
        $.each(currentIframeDocument.find("td[class='Wwday']"),
            function (i, n) {
                var workDays = $("#hdnWdata" + currentDataM).val().split(",");
                for (var j = 0; j < workDays.length; j++) {
                    if ($(n).text() == workDays[j]) {
                        $(n).attr("class", "Wday");
                        $(n).mouseover(function (event) { this.className = 'WdayOn' })
                        $(n).mouseout(function (event) { this.className = 'Wday' });
                    }
                }
            }
        );
        $.each(currentIframeDocument.find("td[class='Wday']"),
            function (i, n) {
                var freeDays = $("#hdnWwdata" + currentDataM).val().split(",");
                for (var k = 0; k < freeDays.length; k++) {
                    if ($(n).text() == freeDays[k]) {
                        $(n).attr("class", "Wwday");
                        $(n).mouseover(function (event) { this.className = 'WwdayOn' })
                        $(n).mouseout(function (event) { this.className = 'Wwday' });
                    }
                }

            }
        );
    }
    //初始化节假日
    function InitSelect() {
        var k = 0;
        try {
            for (k = 0; k < 12; k++) {
                var iObj = $($("iframe")[k].contentDocument);
                debugger
                $.each(iObj.find("td[class='Wday']"),
                        function (i, n) {
                            var freeDays = $("#hdnWwdata" + (k + 1)).val().split(",");
                            var j = 0;
                            for (j = 0; j < freeDays.length; j++) {
                                if ($(n).text() == freeDays[j]) {
                                    $(n).attr("class", "Wwday");
                                    $(n).mouseover(function (event) { this.className = 'WwdayOn' })
                                    $(n).mouseout(function (event) { this.className = 'Wwday' });
                                }
                            }
                        }
                       );
                $.each(iObj.find("td[class='Wwday']"),
                      function (i, n) {
                          var workDays = $("#hdnWdata" + (k + 1)).val().split(",");
                          var j = 0;
                          for (j = 0; j < workDays.length; j++) {
                              if ($(n).text() == workDays[j]) {
                                  $(n).attr("class", "Wday");
                                  $(n).mouseover(function (event) { this.className = 'WdayOn' })
                                  $(n).mouseout(function (event) { this.className = 'Wday' });
                              }
                          }
                      }
                     );
            }
        } catch (err) {
            document.getElementById("lblMsg").innerText = "Initialize data fail, please try it again";
        } finally {
            $.unblockUI();
        }

    }

    //添加休假日
    function AddWwDate_Click() {
        if (check()) {
            debugger
            var obj = currentIframeDocument.find("td[class='Wselday']");
            obj.attr("class", "Wwday");
            obj.mouseover(function (event) { this.className = 'WwdayOn' });
            obj.mouseout(function (event) { this.className = 'Wwday' });
            AddFreeDay();
            $("#txtSelectDate").val("");
        } else {
            //document.getElementById("lblMsg").innerText = "please select date";
        }
    }
    //添加工作日
    function AddWDate_Click() {
        if (check()) {
            var obj = currentIframeDocument.find("td[class='Wselday']");
            obj.attr("class", "Wday");
            obj.mouseover(function (event) { this.className = 'WdayOn' })
            obj.mouseout(function (event) { this.className = 'Wday' });
            AddWorkDay();
            $("#txtSelectDate").val("");
        } else {
           // document.getElementById("lblMsg").innerText = "please select date";
        }
    }
    function check() {
        if ($("#txtSelectDate").val() == "") {
            return false;
        } else {
            return true;
        }
    }
    function AddWorkDay() {
        var workDays = $("#hdnWdata" + currentDataM).val().split(",");
        var i = 0;
        var count = workDays.length;
        for (i = 0; i < workDays.length; i++) {
            if (currentDataD == workDays[i]) {
                workDays.splice(i, 1);
                break;
            }
        }
        if (i == count) {
            workDays.push(currentDataD);
        }
        $("#hdnWdata" + currentDataM).val(workDays);
    }

    function AddFreeDay() {
        var freeDays = $("#hdnWwdata" + currentDataM).val().split(",");
        var i = 0;
        var count = freeDays.length;
        for (i = 0; i < freeDays.length; i++) {
            if (currentDataD == freeDays[i]) {
                break;
            }
        }
        if (i == count) {
            freeDays.push(currentDataD);
        }
        $("#hdnWwdata" + currentDataM).val(freeDays);

    }
    function UpdateFreeDay() {
        var freeDays = $("#hdnWwdata" + currentDataM).val().split(",");
        var i = 0;
        var count = freeDays.length;
        for (i = 0; i < freeDays.length; i++) {
            if (currentDataD == freeDays[i]) {
                freeDays.splice(i, 1);
                break;
            }
        }
        if (i == count) {
            document.getElementById("lblMsg").innerText = "add " + currentDataD;
            freeDays.push(currentDataD);
        }
        $("#hdnWwdata" + currentDataM).val(freeDays);

    }
    //保存至服务器，节假日
    function saveworkdate() {
        var post = {
            Wwmonth1: $('#hdnWwdata1').val(),
            Wwmonth2: $('#hdnWwdata2').val(),
            Wwmonth3: $('#hdnWwdata3').val(),
            Wwmonth4: $('#hdnWwdata4').val(),
            Wwmonth5: $('#hdnWwdata5').val(),
            Wwmonth6: $('#hdnWwdata6').val(),
            Wwmonth7: $('#hdnWwdata7').val(),
            Wwmonth8: $('#hdnWwdata8').val(),
            Wwmonth9: $('#hdnWwdata9').val(),
            Wwmonth10: $('#hdnWwdata10').val(),
            Wwmonth11: $('#hdnWwdata11').val(),
            Wwmonth12: $('#hdnWwdata12').val(),

            Wmonth1: $('#hdnWdata1').val(),
            Wmonth2: $('#hdnWdata2').val(),
            Wmonth3: $('#hdnWdata3').val(),
            Wmonth4: $('#hdnWdata4').val(),
            Wmonth5: $('#hdnWdata5').val(),
            Wmonth6: $('#hdnWwdata6').val(),
            Wmonth7: $('#hdnWdata7').val(),
            Wmonth8: $('#hdnWdata8').val(),
            Wmonth9: $('#hdnWdata9').val(),
            Wmonth10: $('#hdnWdata10').val(),
            Wmonth11: $('#hdnWdata11').val(),
            Wmonth12: $('#hdnWdata12').val(),
        };
        $.post('<%=basePath%>/system/workDate/saveDate?id=' + currentDataY, post, function (data) {
            if (data == "") {
                ERP_alert("保存成功");
                initedata(currentDataY);
                setTimeout("InitSelect()", 2000);
            }
            else
                ERP_alertError("保存失败！" + data);
        });
    }
    //初始化某年的保存数据
    function initedata(year) {
        $.get('<%=basePath%>/system/workDate/getDate?id=' + year, function (data) {
            $('#hdnWwdata1').val(data.Wwmonth1);
            $('#hdnWwdata2').val(data.Wwmonth2);
            $('#hdnWwdata3').val(data.Wwmonth3);
            $('#hdnWwdata4').val(data.Wwmonth4);
            $('#hdnWwdata5').val(data.Wwmonth5);
            $('#hdnWwdata6').val(data.Wwmonth6);
            $('#hdnWwdata7').val(data.Wwmonth7);
            $('#hdnWwdata8').val(data.Wwmonth8);
            $('#hdnWwdata9').val(data.Wwmonth9);
            $('#hdnWwdata10').val(data.Wwmonth10);
            $('#hdnWwdata11').val(data.Wwmonth11);
            $('#hdnWwdata12').val(data.Wwmonth12);
            
            $('#hdnWdata1').val(data.Wmonth1);
            $('#hdnWdata2').val(data.Wmonth2);
            $('#hdnWdata3').val(data.Wmonth3);
            $('#hdnWdata4').val(data.Wmonth4);
            $('#hdnWdata5').val(data.Wmonth5);
            $('#hdnWdata6').val(data.Wmonth6);
            $('#hdnWdata7').val(data.Wmonth7);
            $('#hdnWdata8').val(data.Wmonth8);
            $('#hdnWdata9').val(data.Wmonth9);
            $('#hdnWdata10').val(data.Wmonth10);
            $('#hdnWdata11').val(data.Wmonth11);
            $('#hdnWdata12').val(data.Wmonth12);
        });
    }
</script>
